package com.health.db.service.order.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.health.db.dao.order.IEPayCashPaymentDao;
import com.health.db.dao.user.IUserAccountJournalDao;
import com.health.db.model.EPayCashPayment;
import com.health.db.model.EPayCashPaymentExample;
import com.health.db.model.RegisterOrder;
import com.health.db.model.UserAccountJournal;
import com.health.db.service.order.IEPayCashPaymentService;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;

public class EPayCashPaymentServiceImpl implements IEPayCashPaymentService {
	
	private IEPayCashPaymentDao ePayCashPaymentDao;
	private IUserAccountJournalDao userAccountJournalDao ;


	@Override
	public int insert(EPayCashPayment record) {
		// TODO Auto-generated method stub
		return ePayCashPaymentDao.insert(record);
	}


	@Override
	public List<EPayCashPayment> selectByOrderId(Long recid) {
		// TODO Auto-generated method stub
		return ePayCashPaymentDao.selectByOrderId(recid);
	}
	

	/**
	 * @return the userAccountJournalDao
	 */
	public IUserAccountJournalDao getUserAccountJournalDao() {
		return userAccountJournalDao;
	}


	/**
	 * @param userAccountJournalDao the userAccountJournalDao to set
	 */
	public void setUserAccountJournalDao(
			IUserAccountJournalDao userAccountJournalDao) {
		this.userAccountJournalDao = userAccountJournalDao;
	}


	public IEPayCashPaymentDao getePayCashPaymentDao() {
		return ePayCashPaymentDao;
	}

	public void setePayCashPaymentDao(IEPayCashPaymentDao ePayCashPaymentDao) {
		this.ePayCashPaymentDao = ePayCashPaymentDao;
	}

	@Override
	public int updateByPayCashIDSelective(EPayCashPayment updateRecord) {
		
		int result = -1;
		Date now=DateUtils.now();
		updateRecord.setUpdateDate(now);
		// TODO Auto-generated method stub
		result= ePayCashPaymentDao.updateByPayCashIDSelective(updateRecord);
		
		return result;
	}
	
	@Override
	public int updateByPayCashIDSelective(EPayCashPayment record,EPayCashPayment updateRecord,RegisterOrder  orderInfo) {
		
		int result = -1;
		Date now=DateUtils.now();
		updateRecord.setUpdateDate(now);
		// TODO Auto-generated method stub
		result= ePayCashPaymentDao.updateByPayCashIDSelective(updateRecord);
		
		UserAccountJournal accountJournal = new UserAccountJournal();
		
		if (null==orderInfo.getPayTypeId()) {
			
		}else if (Constant.CONSTANTS_PAYMENT_WEIXIN==orderInfo.getPayTypeId().shortValue()){
			accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_3+"订单号"+record.getPaycashIdno()+"R");
			accountJournal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_3);
		}else if(Constant.CONSTANTS_PAYMENT_ALIPAY==orderInfo.getPayTypeId().shortValue()){
			accountJournal.setJournalDesc(Constant.CONSTANTS_VOUCHERNAME_5+"订单号"+record.getPaycashIdno()+"R");
			accountJournal.setVoucherName(Constant.CONSTANTS_VOUCHERNAME_5);
		}
		
		accountJournal.setAcctpageid(null);
		accountJournal.setAccountid(orderInfo.getUserid());
		accountJournal.setAccountIdno(orderInfo.getUserContact());
		accountJournal.setAccountName(orderInfo.getUserName());
		accountJournal.setVoucherid(record.getVoucherid().toString());
		accountJournal.setVoucherIdno(orderInfo.getRegisterNo());
		accountJournal.setJournalTotal(record.getPaycashTotal().multiply(new BigDecimal(-1)));
		
		accountJournal.setCreateUserid(orderInfo.getUserid());
		accountJournal.setUpdateUserid(orderInfo.getUserid());
		accountJournal.setVoucherDate(now);
		accountJournal.setVouchertypeid(Constant.CONSTANTS_PAY_ACTION_ID_31);
		accountJournal.setSourceid(Constant.CONSTANTS_PAY_ACTION_ID_31);
		accountJournal.setComment(record.getPaycashIdno());
		userAccountJournalDao.insert(accountJournal);
		
		return result;
	}


	@Override
	public List<EPayCashPayment> selectByExample(EPayCashPaymentExample example) {
		// TODO Auto-generated method stub
		return ePayCashPaymentDao.selectByExample(example);
	}

}
